package com.ruoyi.web.controller.approve;

import java.util.HashMap;
import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.ExaLeave;
import com.ruoyi.service.IExaLeaveService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请假】Controller
 * 
 * @author ruoyi
 * @date 2020-07-01
 */
@Controller
@RequestMapping("/ruoyi/leave")
public class ExaLeaveController extends BaseController
{
    private String prefix = "ruoyi/leave";

    @Autowired
    private IExaLeaveService exaLeaveService;


    @RequiresPermissions("ruoyi:leave:view")
    @GetMapping()
    public String leave()
    {
        return prefix + "/leave";
    }

    /**
     * 查询【请假】列表
     */
    @RequiresPermissions("ruoyi:leave:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ExaLeave exaLeave)
    {
        startPage();
        String loginName = ShiroUtils.getLoginName();
       /* List<ExaLeave> list = exaLeaveService.selectExaLeaveList(exaLeave);*/
        List<ExaLeave> list = exaLeaveService.selectExaLeaveByloginName(loginName);
        return getDataTable(list);
    }

    /**
     * 导出【请假】列表
     */
    @RequiresPermissions("ruoyi:leave:export")
    @Log(title = "【请假】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ExaLeave exaLeave)
    {
        List<ExaLeave> list = exaLeaveService.selectExaLeaveList(exaLeave);
        ExcelUtil<ExaLeave> util = new ExcelUtil<ExaLeave>(ExaLeave.class);
        return util.exportExcel(list, "leave");
    }

    /**
     * 新增【请假】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请假】
     */
    @RequiresPermissions("ruoyi:leave:add")
    @Log(title = "【请假】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ExaLeave exaLeave)
    {
        exaLeave.setlDepa(ShiroUtils.getdept());
        exaLeave.setlPicture(ShiroUtils.getavatar());
        exaLeave.setlCreator(ShiroUtils.getLoginName());
        exaLeave.setlTheHeir(ShiroUtils.getLoginName());
        return toAjax(exaLeaveService.insertExaLeave(exaLeave));
    }

    /**
     * 修改【请假】
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ExaLeave exaLeave = exaLeaveService.selectExaLeaveById(id);
        mmap.put("exaLeave", exaLeave);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请假】
     */
    @RequiresPermissions("ruoyi:leave:edit")
    @Log(title = "【请假】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ExaLeave exaLeave)
    {
        return toAjax(exaLeaveService.updateExaLeave(exaLeave));
    }

    /**
     * 删除【请假】
     */
    @RequiresPermissions("ruoyi:leave:remove")
    @Log(title = "【请假】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(exaLeaveService.deleteExaLeaveByIds(ids));
    }

    /**
     * 提交申请
     */
    @Log(title = "请假业务", businessType = BusinessType.UPDATE)
    @PostMapping( "/submitApply")
    @ResponseBody
    public AjaxResult submitApply(Long id) {
        int leave = exaLeaveService.submitExaStatus(id);
        String applyUserId = ShiroUtils.getLoginName();

        return success();
    }
    /**
     * 撤销申请
     */
    @Log(title = "请假业务", businessType = BusinessType.UPDATE)
    @PostMapping( "/revocation")
    @ResponseBody
    public AjaxResult revocation(Long id) {
        int leave = exaLeaveService.revoExaStatus(id);
        String applyUserId = ShiroUtils.getLoginName();

        return success();
    }
    /**
     * 销假
     */
    @Log(title = "请假业务", businessType = BusinessType.UPDATE)
    @PostMapping( "/reports")
    @ResponseBody
    public AjaxResult reports(Long id) {
        int leave = exaLeaveService.repExaStatus(id);
        String applyUserId = ShiroUtils.getLoginName();

        return success();
    }
}
